package com.deYang.app.ljodfh.ui.activities;

import android.content.Intent;
import android.content.res.Configuration;
import android.os.Bundle;
import android.support.annotation.IdRes;
import android.support.annotation.NonNull;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;

import com.jiongbull.jlog.JLog;
import com.deYang.app.ljodfh.ui.fragment.RebllionFragment;
import com.ruihang.generalibrary.utils.SharedPreferencesUtil;
import com.deYang.app.ljodfh.R;
import com.deYang.app.ljodfh.base.BaseActivity;
import com.deYang.app.ljodfh.base.BaseFragment;
import com.deYang.app.ljodfh.ui.fragment.HomeFragment;
import com.deYang.app.ljodfh.ui.fragment.MineFragment;
import com.deYang.app.ljodfh.ui.fragment.TopicFragment;
import com.deYang.app.ljodfh.ui.utils.IHomeNetCallBack;
import com.deYang.app.ljodfh.ui.widget.BasePlayerView;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.jpush.android.api.JPushInterface;

public class MainActivity extends BaseActivity implements RadioGroup.OnCheckedChangeListener, IHomeNetCallBack {
    @BindView(R.id.main_bottom_lay)
    protected RadioGroup bottom_lay;
    @BindView(R.id.main_home)
    protected RadioButton home_rb;
    @BindView(R.id.main_topic)
    protected RadioButton topic_rb;
    @BindView(R.id.main_rebllion)
    protected RadioButton rebllion_rb;
    @BindView(R.id.main_mine)
    protected RadioButton mine_rb;
    @BindView(R.id.main_top_lay)
    protected ViewGroup topLay;

    private BaseFragment showingFragment;
    private FragmentManager fragmentManager;
    private HomeFragment homeFragment;
    private MineFragment mineFragment;
    private TopicFragment topicFragment;
    private RebllionFragment rebllionFragment;

    private String playBackUrl;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initData();
        initPush();
    }

    private void initPush() {
        boolean isPush = (boolean) SharedPreferencesUtil.getParam(this, "PUSH_KEY", true);
        if (isPush) {
            SharedPreferencesUtil.setParam(this, "PUSH_KEY", true);
            JPushInterface.resumePush(this);
            JPushInterface.setAlias(this, 0, "scgc2_0");
        } else {
            JPushInterface.stopPush(this);
        }


    }

    @Override
    protected void initInject() {
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
    }

    private void initData() {
        bottom_lay.setOnCheckedChangeListener(this);
        fragmentManager = getSupportFragmentManager();
        home_rb.setChecked(true);

        //setTestHistories();
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        if (intent.hasExtra(BaseActivity.EX_ID)) {
            if (homeFragment != null) {
                homeFragment.setCurrentSelectIndex();
            }
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        //int i = bottomBar.getCurrentSelectedPosition();
        if (showingFragment == null) {
            onCheckedChanged(bottom_lay, R.id.main_home);
        } else {
            if (showingFragment instanceof MineFragment) {
                mine_rb.setChecked(true);
            } else if (showingFragment instanceof TopicFragment) {
                topic_rb.setChecked(true);
            } else if (showingFragment instanceof RebllionFragment) {
                rebllion_rb.setChecked(true);
            } else {
                home_rb.setChecked(true);
            }
        }
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
//            bottom_lay.setVisibility(View.GONE);
        } else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) {
//            bottom_lay.setVisibility(View.VISIBLE);
        }
    }

    @Override
    public void onCheckedChanged(RadioGroup group, @IdRes int checkedId) {
        switch (checkedId) {
            case R.id.main_home: //首页
                if (homeFragment == null) {
                    homeFragment = new HomeFragment();
                }
                findViewById(R.id.main_search_iv).setVisibility(View.VISIBLE);
                findViewById(R.id.main_look_back_tv).setVisibility(View.VISIBLE);
                homeFragment.setOthersHeight(topLay.getHeight());
                showFragment(homeFragment);
                break;
            case R.id.main_topic:  //专题
                if (topicFragment == null) {
                    topicFragment = new TopicFragment();
                }
                findViewById(R.id.main_search_iv).setVisibility(View.GONE);
                findViewById(R.id.main_look_back_tv).setVisibility(View.GONE);
                showFragment(topicFragment);
                break;
            case R.id.main_rebllion:
                if (rebllionFragment == null) {
                    rebllionFragment = new RebllionFragment();
                }
                findViewById(R.id.main_search_iv).setVisibility(View.GONE);
                findViewById(R.id.main_look_back_tv).setVisibility(View.GONE);
                showFragment(rebllionFragment);
                break;
            case R.id.main_mine:   //我的
                if (mineFragment == null) {
                    mineFragment = new MineFragment();
                }
                findViewById(R.id.main_search_iv).setVisibility(View.GONE);
                findViewById(R.id.main_look_back_tv).setVisibility(View.GONE);
                showFragment(mineFragment);
                break;
        }
    }

    @OnClick({R.id.main_search_iv, R.id.main_look_back_tv})
    public void topClick(View view) {
        switch (view.getId()) {
            case R.id.main_search_iv:
                Intent searchIntent = new Intent(this, SearchActivity.class);
                startActivity(searchIntent);
                break;
            case R.id.main_look_back_tv:
                if (!TextUtils.isEmpty(playBackUrl) && playBackUrl.endsWith(".json")) {
                    Intent playBackIntent = new Intent(this, LookBackActivity.class);
                    playBackIntent.putExtra(BaseActivity.EX_URL, playBackUrl);
                    startActivity(playBackIntent);
                } else {
                    toast("数据错误");
                }

                break;
        }
    }

    private void showFragment(final BaseFragment fragment) {
        if (fragment == null || showingFragment == fragment) {
            JLog.e("show fragment faile fragment :" + fragment + "  showingFragment:" + showingFragment);
            return;
        }
        FragmentTransaction ft = fragmentManager.beginTransaction();
        if (showingFragment != null && showingFragment.isAdded()) {
            ft.hide(showingFragment);
        }

        if (fragment.isAdded()) {
            ft.show(fragment);
        } else {
            ft.add(R.id.main_content, fragment);
        }
        ft.commit();
        showingFragment = fragment;
        JLog.e("show fragment->" + fragment);
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
    }

    @Override
    public void onBackPressed() {
        if (showingFragment == null || !showingFragment.onBackPressed()) {
            exit();
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    }

    @Override
    public void setOtherUrls(int mode, String... urls) {
        if (mode == MODE_PLAYBACK) {
            if (urls != null && urls.length > 0) {
                playBackUrl = urls[0];
            }
        }
    }


    //退出时的时间
    private long mExitTime;

    public void exit() {
        if ((System.currentTimeMillis() - mExitTime) > 2000) {
            Toast.makeText(MainActivity.this, "再按一次退出应用", Toast.LENGTH_SHORT).show();
            mExitTime = System.currentTimeMillis();
        } else {
            if (showingFragment == null || !showingFragment.hasShowPlayer()) {
                BasePlayerView.releaseAllVideos();
            }
            if (showingFragment != null) {
                showingFragment.willDoExit();
            }
            Intent mHomeIntent = new Intent(Intent.ACTION_MAIN);
            mHomeIntent.addCategory(Intent.CATEGORY_HOME);
            mHomeIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK
                    | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
            startActivity(mHomeIntent);
        }
    }

}

